﻿using System;
using System.Collections.Generic;
using System.Text;
using IBatisNet.Common.Utilities;
using IBatisNet.DataAccess;
using IBatisNet.DataAccess.Configuration;

namespace Wolfut.MirageEE.Core.Service
{
    /// <summary>
    /// Summary description for ServiceConfig.
    /// </summary>
    public class ServiceConfig
    {
        static private object _synRoot = new Object();
        static private ServiceConfig _instance;

        private IDaoManager _daoManager = null;

        /// <summary>
        /// Remove public constructor. prevent instantiation.
        /// </summary>
        private ServiceConfig() { }
         static public ServiceConfig GetInstance()
         {
             return GetInstance("SqlMapDao");
         }
         static public ServiceConfig GetInstance(string contextName)
        {
           
                if (_instance == null)
                {
                    lock (_synRoot)
                    {
                        if (_instance == null)
                        {
                            ConfigureHandler handler = new ConfigureHandler(ServiceConfig.Reset);
                            try
                            {
                                DomDaoManagerBuilder builder = new DomDaoManagerBuilder();
                                builder.Configure();

                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                            }

                            _instance = new ServiceConfig();
                            _instance._daoManager = DaoManager.GetInstance(contextName);
                        }
                    }
                }

         
             return _instance;
        }


        /// <summary>
        /// Reset the singleton
        /// </summary>
        /// <remarks>
        /// Must verify ConfigureHandler signature.
        /// </remarks>
        /// <param name="obj">
        /// </param>
        static public void Reset(object obj)
        {
            _instance = null;
        }

        public IDaoManager MyDaoManager
        {
            get
            {
                return _daoManager;
            }
        }

    }
}
